#include "OiPCH.hpp"

namespace Oi
{
    namespace PLScannerCore
    {
        class ToolLabelImpl : public Impl<ToolLabel>
        {
        public:
            void init();

        public:
            QString _name;
            QVariantMap _value;
        };

        void ToolLabelImpl::init()
        {
          
        }

        ToolLabel::ToolLabel(QWidget * parent /*= nullptr */) : QWidget(parent)
        {
            OI_I(ToolLabel) ->init();
            //setMinimumHeight(50);
            setMaximumHeight(150);
        }

        void ToolLabel::setValue(QVariantMap value)
        {
            OI_IMPL()->_value = value;
            update();
        }

        void ToolLabel::setText(QString text)
        {
            OI_IMPL()->_name = text;
        }

        void ToolLabel::paintEvent(QPaintEvent *event)
        {
            OI_F(ToolLabel);

            QPainter painter(this);

            auto rect = event->rect();

            auto font = painter.font();
            font.setPointSize(10);
            painter.setFont(font);
            
            if(d->_value.contains("result") && d->_value["result"].toBool())
            {
                painter.fillRect(event->rect().adjusted(0, 0, 0, 0), QColor("#00FF00"));
                painter.setPen(QPen(Qt::black, 2));
            }
            else
            {
                painter.fillRect(event->rect().adjusted(0, 0, 0, 0), QColor("#FF0000"));
                painter.setPen(QPen(Qt::white, 2));
            }

            painter.drawText(5, 15, d->_name );
            double nh  = QFontMetrics(font).height();

            painter.drawLine(QPointF(4, 25), QPointF(rect.width() - 4, 25));

            font.setPointSize(14);
            painter.setFont(font);

            double h = 35;
            for(auto iter = d->_value.begin(); iter != d->_value.end(); iter++)
            {
                if(iter.key() != "result" && iter.key() != "Value")
                {
                    auto text = iter.value().toString();
                    h += QFontMetrics(font).height();
                    painter.drawText(8, h, iter.key());
                    painter.drawText(rect.width() - 8 - QFontMetrics(font).width(text), h, text);
                    h += 8;
                }
            }
        }

    }
}